package com.example.andengineScene;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.particle.SpriteParticleSystem;
import org.andengine.entity.particle.emitter.CircleParticleEmitter;
import org.andengine.entity.particle.initializer.AccelerationParticleInitializer;
import org.andengine.entity.particle.initializer.ColorParticleInitializer;
import org.andengine.entity.particle.modifier.AlphaParticleModifier;
import org.andengine.entity.particle.modifier.RotationParticleModifier;
import org.andengine.entity.particle.modifier.ScaleParticleModifier;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.extension.texturepacker.opengl.texture.util.texturepacker.TexturePackTextureRegionLibrary;
import org.andengine.extension.texturepacker.opengl.texture.util.texturepacker.TexturePackerTextureRegion;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.modifier.ease.EaseSineInOut;

import android.graphics.Typeface;
import com.example.andengineConstant.GameConstant;
import com.example.andenginegame.MainActivity;
import com.example.andenginegame.Style;

public class PlayScene extends Scene {
	private long particleStart_time; 
	private SpriteParticleSystem particleSystem;
	private boolean isaddParticle = false;
    private boolean isFirstOnclick = true;
    private boolean isStartPrefect = true;
    private int start_prefect = 0;
    private long play_start_time;
	private long playTime=0;
	private Font scoreFont;
	private Text scoreText;
	private Style style;
	private int maxScore;
	private int score;
	private Font hintFont;//��ʾ��Ϣ
	private Text hintText;
	Text missionText;
	private Font targetScoreHintFont;
	private Text targetScoreHintText;
	private Font scoreSingleOrDoubleFont;
	private Text scoreSingleOrDoubleText;
	Text scoreOfmissionText;
	Text didMissionText;
	public int[] data = new int[12];
	Sprite missionbackground;
	
	private int missNeedNum;
	private int missdidNum;
	
	public Text getMissionText() {
		return missionText;
	}
	public void setMissionText(Text missionText) {
		this.missionText = missionText;
		this.attachChild(this.missionText);
	}
	public Font getTargetScoreHintFont() {
		return targetScoreHintFont;
	}
	public void setTargetScoreHintFont(Font targetScoreHintFont) {
		this.targetScoreHintFont = targetScoreHintFont;
		this.targetScoreHintFont.load();
	}
	public Text getTargetScoreHintText() {
		return targetScoreHintText;
	}
	public void setTargetScoreHintText(Text targetScoreHintText) {
		this.targetScoreHintText = targetScoreHintText;
		this.targetScoreHintText.setScale(act.xscale, act.yscale);
		this.attachChild(this.targetScoreHintText);
	}
	public Font getScoreSingleOrDoubleFont() {
		return scoreSingleOrDoubleFont;
	}
	public void setScoreSingleOrDoubleFont(Font scoreSingleOrDoubleFont) {
		this.scoreSingleOrDoubleFont = scoreSingleOrDoubleFont;
		this.scoreSingleOrDoubleFont.load();
	}
	public Text getScoreSingleOrDoubleText() {
		return scoreSingleOrDoubleText;
	}
	public void setScoreSingleOrDoubleText(Text scoreSingleOrDoubleText) {
		this.scoreSingleOrDoubleText = scoreSingleOrDoubleText;
		this.scoreSingleOrDoubleText.setScale(act.xscale, act.yscale);
		this.attachChild(this.scoreSingleOrDoubleText);
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}

	private Sprite playRole;
	private Sprite targetRole;
	
	private float targetRole_h;
	private int targetRole_speed;
	private int playRole_speed;
	
	private int prefectNum = 0;
	private int continue_prefectNum;
	private MainActivity act;
	protected boolean istrue = true;
	protected Text continueText;
	protected Sprite ContinueBackground;
	protected Sprite close;
	protected Sprite text;
	protected Sprite alertW;
	private TexturePackerTextureRegion playRoleTexture;
	protected Text HintText;
	protected Text prefectText;
	public Font getScoreFont() {
		return scoreFont;
	}
	public void setScoreFont(Font scoreFont) {
		this.scoreFont = scoreFont;
		this.scoreFont.load();
	}
	public Text getScoreText() {
		return scoreText;
	}
	public void setScoreText(Text scoreText) {
		this.scoreText = scoreText;
		this.scoreText.setScale(act.xscale,act.yscale);
		this.attachChild(this.scoreText);
	}
	public int getMaxScore() {
		return maxScore;
	}
	public void setMaxScore(int maxScore) {
		this.maxScore = maxScore;
	}
	public Font getHintFont() {
		return hintFont;
	}
	public void setHintFont(Font hintFont) {
		this.hintFont = hintFont;
		this.hintFont.load();
	}
	public Text getHintText() {
		return hintText;
	}
	public void setHintText(Text hintText) {
		this.hintText = hintText;
		this.hintText.setScale(act.xscale,act.yscale);
		this.attachChild(this.hintText);
	}
	public Sprite getPlayRole() {
		return playRole;
	}
	public void setPlayRole(Sprite playRole) {
		this.playRole = playRole;
		this.playRole.setScale(act.xscale,act.yscale);
		this.attachChild(this.playRole);
	}
	public Sprite getTargetRole() {
		return targetRole;
	}
	public void setTargetRole(Sprite targetRole) {
		this.targetRole = targetRole;
		this.targetRole.setScale(act.xscale,act.yscale);
		this.attachChild(this.targetRole);
	}
	public float getTargetRole_h() {
		return targetRole_h;
	}
	public void setTargetRole_h(float targetRole_h) {
		this.targetRole_h = targetRole_h;
	}
	public int getTargetRole_speed() {
		return targetRole_speed;
	}
	public void setTargetRole_speed(int targetRole_speed) {
		this.targetRole_speed = targetRole_speed;
	}
	public int getPlayRole_speed() {
		return playRole_speed;
	}
	public void setPlayRole_speed(int playRole_speed) {
		this.playRole_speed = playRole_speed;
	}
	public void setStyle(Style style) {
		this.style = style;
		
	}
	public Style getStyle(){
		return this.style;
	}
	
	public PlayScene (final MainActivity act) {
		this.act = act;
		
	
		missNeedNum = act.mainScene.needNum;
		missdidNum = act.mainScene.didNum;
		
		this.targetScoreHintFont = FontFactory.create(
				act.getFontManager(), act.getTextureManager(), 
				256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20,Color.WHITE.getABGRPackedInt()
				);
		setTargetScoreHintFont(this.targetScoreHintFont);
		String targetScore = act.mainScene.style.getName()+"  "+act.mainScene.style.getScore()+"分/击";
        this.targetScoreHintText = new Text(50, 50,this.targetScoreHintFont,targetScore,"00000000000".length(),new TextOptions(HorizontalAlign.CENTER),act.getVertexBufferObjectManager());
		setTargetScoreHintText(this.targetScoreHintText);
		
		this.scoreSingleOrDoubleFont = FontFactory.create(act.getFontManager(), act.getTextureManager(),
				100, 100, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20,Color.WHITE.getABGRPackedInt());
		setScoreSingleOrDoubleFont(this.scoreSingleOrDoubleFont);
		
		this.scoreSingleOrDoubleText = new Text(act.CAMERA_WIDTH-150, 50, this.scoreSingleOrDoubleFont, "单倍得分 ", act.getVertexBufferObjectManager());
		setScoreSingleOrDoubleText(this.scoreSingleOrDoubleText);
		
		
		this.hintFont = FontFactory.create(act.getFontManager(), act.getTextureManager(),
				450,100, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 40,Color.WHITE.getABGRPackedInt());
		
		setHintFont(this.hintFont);
		
	 	//����������ʾ
		 TextureRegion missionTexture = act.ur3.get(23);
		 
		 final float missionX = (act.CAMERA_WIDTH-missionTexture.getWidth())/2;
		 missionbackground = new Sprite(missionX,act.CAMERA_HEIGH-200,missionTexture,act.getVertexBufferObjectManager());
		 missionbackground.setColor(GameConstant.UNDER_COLOR_R,GameConstant.UNDER_COLOR_G,GameConstant.UNDER_COLOR_B);
		 missionbackground.setScale(act.xscale, act.yscale);
		 this.attachChild(missionbackground);
		
		 	
	 	String missionString = GameConstant.MISSIONNAME+act.mainScene.getMissionId()+GameConstant.MISSIONNUM_PID;
	 
	 	Font missionFont = FontFactory.create(act.getFontManager(), act.getTextureManager(), 
				512, 512, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20, Color.WHITE.getARGBPackedInt());
	     missionFont.load();
		
	 	 missionText = new Text(missionbackground.getX()+50,missionbackground.getY(),
		 missionFont, missionString,Character.MAX_VALUE, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
	     missionText.setPosition(missionbackground.getX()+50, missionbackground.getY()+5);
	    
	     this.attachChild(missionText);
	 	 
	 	
	   
	    Font scoreOfmissionFont = FontFactory.create(act.getFontManager(),act.getTextureManager(), 
				1024, 1024, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 30, Color.WHITE.getARGBPackedInt());
	    scoreOfmissionFont.load();
	   
	    String missionType =  act.mainScene.missis.get(act.mainScene.getMissionId()-1).getType();
	    String desc = "";
	    
	     for (int i = 0; i <act.mainScene.missiTypes.size(); i++) {
			if(act.mainScene.missiTypes.get(i).getType().equals(missionType))
				desc = act.mainScene.missiTypes.get(i).getDesc();
		 }
	   
	     scoreOfmissionText = new Text(missionbackground.getX()+50,missionbackground.getY()+50,
				 scoreOfmissionFont, desc.replace("{0}",act.mainScene.needNum+""),Character.MAX_VALUE, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
	     this.attachChild(scoreOfmissionText);
	     int didNum = act.missionFinishNum;
	     String didString = didNum+"/"+act.mainScene.needNum;
	    
	     didMissionText = new Text(0,385,
	    		 scoreOfmissionFont, didString,Character.MAX_RADIX, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
	     didMissionText.setPosition(this.missionbackground.getX()+this.missionbackground.getWidth()-didMissionText.getWidth()-50, this.missionbackground.getY()+this.missionbackground.getHeight()/2);
        
	     this.attachChild(didMissionText);
       
		
		
		this.hintText = new Text(act.CAMERA_WIDTH/2, act.CAMERA_HEIGH/2, this.hintFont, "点击屏幕射中目标得分", act.getVertexBufferObjectManager());
		setHintText(this.hintText);
		this.hintText.setPosition((act.CAMERA_WIDTH-this.hintText.getWidth())/2,act.CAMERA_HEIGH/2);
		this.scoreFont =  FontFactory.create(act.getFontManager(), act.getTextureManager(), 
				256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 100,Color.WHITE.getABGRPackedInt());
		setScoreFont(this.scoreFont);
	    this.setBackground(new Background(act.mainScene.style.getBg_r(), act.mainScene.style.getBg_g(),act.mainScene.style.getBg_b()));
		this.scoreText = new Text((act.CAMERA_WIDTH-80)/2, 200, this.scoreFont,"0","00000000".length(),new TextOptions(HorizontalAlign.CENTER),act.getVertexBufferObjectManager());
		this.scoreText.setHorizontalAlign(HorizontalAlign.CENTER);
		
		setScoreText(this.scoreText);
	   		
		TexturePackTextureRegionLibrary texLibraryPlay = act.ur3;
		if(GameConstant.paly_whichOfImage[act.mainScene.style.getId()]==3)
			texLibraryPlay= act.ur3;
		if(GameConstant.paly_whichOfImage[act.mainScene.style.getId()]==1)
			texLibraryPlay= act.ur1;
		if(GameConstant.paly_whichOfImage[act.mainScene.style.getId()]==2)
			texLibraryPlay= act.ur2;
	    playRoleTexture = texLibraryPlay.get(GameConstant.play_idofImage[act.mainScene.style.getId()]);
	    final float hit1_X = (act.CAMERA_WIDTH-playRoleTexture.getWidth())/2;
		final float hit1_Y = (act.CAMERA_HEIGH*3/4);

		this.playRole = new Sprite(hit1_X, hit1_Y, playRoleTexture, act.getVertexBufferObjectManager());
		this.playRole.setColor(act.mainScene.style.getMe_r(), act.mainScene.style.getMe_g(), act.mainScene.style.getMe_b());
		setPlayRole(this.playRole);
		TexturePackTextureRegionLibrary texLibraryTarget = act.ur3;
		if(GameConstant.hit_whichOfImage[act.mainScene.style.getId()]==3)
			texLibraryTarget= act.ur3;
		if(GameConstant.hit_whichOfImage[act.mainScene.style.getId()]==1)
			texLibraryTarget= act.ur1;
		if(GameConstant.hit_whichOfImage[act.mainScene.style.getId()]==2)
			texLibraryTarget= act.ur2;
	    TextureRegion targetRoleTexture = texLibraryTarget.get(GameConstant.hit_idofImage[act.mainScene.style.getId()]);
		this.targetRole = new Sprite((act.CAMERA_WIDTH-targetRoleTexture.getWidth()+116)/2,act.CAMERA_HEIGH/2-116,targetRoleTexture,act.getVertexBufferObjectManager());
		this.targetRole.setColor(act.mainScene.style.getTarget_r(),act.mainScene.style.getTarget_g(),act.mainScene.style.getTarget_b());
		this.targetRole.registerEntityModifier(new LoopEntityModifier(
			
				new SequenceEntityModifier(
						new MoveModifier(4, this.targetRole.getWidth()-50,act.CAMERA_WIDTH-this.targetRole.getWidth()-50, this.targetRole.getY(), this.targetRole.getY(),EaseSineInOut.getInstance()),
						new MoveModifier(4, act.CAMERA_WIDTH-this.targetRole.getWidth()-50,this.targetRole.getWidth()-50, this.targetRole.getY(), this.targetRole.getY(),EaseSineInOut.getInstance())
				)
		));
		setTargetRole(this.targetRole);
		
		this.setOnSceneTouchListener(new IOnSceneTouchListener() {

			@Override
			public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		
			switch(pSceneTouchEvent.getAction()){
			
			case TouchEvent.ACTION_DOWN:
				PlayScene.this.playRole.clearEntityModifiers();
				act.launchSound.play();
				if(missdidNum<missNeedNum){
			     	removeHit();
				}
					
				if(isFirstOnclick)
				{
					play_start_time = System.currentTimeMillis();
					isFirstOnclick = false;
												
				}
			 PlayScene.this.getPlayRole().registerEntityModifier(new MoveModifier(PlayScene.this.getPlayRole().getY()/2000, PlayScene.this.getPlayRole().getX(), PlayScene.this.getPlayRole().getX(), PlayScene.this.getPlayRole().getY(), -10));
				
				
			break;
			case TouchEvent.ACTION_UP:
			break;
			case TouchEvent.ACTION_MOVE:
			break;

			}
			return true;
			}

			private void removeHit() {
				if(PlayScene.this.hintText!=null)
				{
					PlayScene.this.hintText.detachSelf();
				    PlayScene.this.hintText = null;
                }
				if(PlayScene.this.missionbackground!=null)
				{
					PlayScene.this.missionbackground.detachSelf();
				    PlayScene.this.missionbackground = null;
                }
				if(PlayScene.this.missionText!=null)
				{
					PlayScene.this.missionText.detachSelf();
				    PlayScene.this.missionText = null;
                }
				if(PlayScene.this.scoreOfmissionText!=null)
				{
					PlayScene.this.scoreOfmissionText.detachSelf();
				    PlayScene.this.scoreOfmissionText = null;
                }
				if(PlayScene.this.didMissionText!=null)
				{
					PlayScene.this.didMissionText.detachSelf();
				    PlayScene.this.didMissionText = null;
                }
						
			}
			});
		
		this.registerUpdateHandler(new IUpdateHandler() {
			@Override
			public void reset() { 
				
			}

			@Override
			public void onUpdate(final float pSecondsElapsed) {
				
				
				if(!isFirstOnclick&&PlayScene.this.playRole.getEntityModifierCount()<=0)
				PlayScene.this.playRole.registerEntityModifier(
						new MoveModifier(8, PlayScene.this.playRole.getX(),PlayScene.this.playRole.getX(), PlayScene.this.playRole.getY(), act.CAMERA_HEIGH+10));
                
				if(isaddParticle)
				{
					if(System.currentTimeMillis()-particleStart_time>=800)
					{
						particleSystem.detachSelf();
						
					}
				}
			// ��ײ��� 
				if(PlayScene.this.getPlayRole().collidesWith(PlayScene.this.getTargetRole()))
				{
				    
					particleStart_time = System.currentTimeMillis();					
					final CircleParticleEmitter particleEmitter = new CircleParticleEmitter(PlayScene.this.getTargetRole().getX(),PlayScene.this.getTargetRole().getY(), 50);
					particleSystem = new SpriteParticleSystem(particleEmitter,50, 50, 250, act.mParticleTextureRegion, act.getVertexBufferObjectManager());
					particleSystem.addParticleInitializer(new ColorParticleInitializer<Sprite>(act.mainScene.style.getTarget_r(),act.mainScene.style.getTarget_g(),act.mainScene.style.getTarget_b()));
					particleSystem.addParticleInitializer(new AccelerationParticleInitializer<Sprite>(-350,350,-350,350) );
					particleSystem.addParticleInitializer(new RotationParticleModifier<Sprite>(0,1, 0.0f, 360.0f));
					particleSystem.addParticleInitializer(new ScaleParticleModifier<Sprite>(0,1, 0.6f,2f));
					particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0, 0.5f, 0, 1));
					particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0.5f,1, 1, 0));
					PlayScene.this.attachChild(particleSystem);
					isaddParticle = true;
				    int hight = (int)(Math.random()*(act.CAMERA_HEIGH/2-150));
				   
				    	hight = act.CAMERA_HEIGH/2-hight;
				    float width = (float) (Math.random()*(act.CAMERA_WIDTH-PlayScene.this.getTargetRole().getWidth()));
				    if(width<=PlayScene.this.getTargetRole().getWidth())
				    	width = PlayScene.this.getTargetRole().getWidth()-50;
				    
				   // PlayScene.this.getTargetRole().setPosition(act.CAMERA_WIDTH/2,hight);
				   
				    int time = (int) (Math.random()*3);
				    if(time>=3)
				    	time = 3;
				    if(time<=1)
				    	time = 1;
					PlayScene.this.getTargetRole().clearEntityModifiers();
					PlayScene.this.targetRole.registerEntityModifier(
							new SequenceEntityModifier(
							new MoveModifier(0.5f, PlayScene.this.targetRole.getWidth()-50,  PlayScene.this.targetRole.getWidth()-50 , -PlayScene.this.getTargetRole().getHeight(), hight),
		                   
							new LoopEntityModifier(
							
							new SequenceEntityModifier(
									new MoveModifier(time, PlayScene.this.targetRole.getWidth()-50,act.CAMERA_WIDTH-PlayScene.this.targetRole.getWidth()-50,hight,hight,EaseSineInOut.getInstance()),
									new MoveModifier(time, act.CAMERA_WIDTH-PlayScene.this.targetRole.getWidth()-50,PlayScene.this.targetRole.getWidth()-50, hight,hight,EaseSineInOut.getInstance())
							)
							)
					));
					
					       
					PlayScene.this.getPlayRole().clearEntityModifiers();
					PlayScene.this.getPlayRole().setPosition(hit1_X, hit1_Y);
					
					score+=act.mainScene.getStyle().getScore();
					act.totalScore+=act.mainScene.getStyle().getScore();
					act.todayScore+=act.mainScene.getStyle().getScore();
					
					PlayScene.this.scoreText.setText(score+"");
					PlayScene.this.scoreText.setPosition((act.CAMERA_WIDTH-PlayScene.this.scoreText.getWidth())/2, 200);
					if(Math.abs(PlayScene.this.getTargetRole().getX()+PlayScene.this.getTargetRole().getWidth()/2-PlayScene.this.getPlayRole().getX()-PlayScene.this.getPlayRole().getWidth()/2)>=0 && Math.abs(PlayScene.this.getTargetRole().getX()+PlayScene.this.getTargetRole().getWidth()/2-PlayScene.this.getPlayRole().getX()-PlayScene.this.getPlayRole().getWidth()/2)<=PlayScene.this.getPlayRole().getWidth()/2)
					{
						
						 Font prefectTextFont = FontFactory.create(act.getFontManager(), act.getTextureManager(), 
									300, 200, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 50, Color.WHITE.getARGBPackedInt());
						 prefectTextFont.load();
							
						  prefectText = new Text(act.CAMERA_WIDTH/2+50,act.CAMERA_HEIGH/3,
						  prefectTextFont, "完美命中",4, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
						  prefectText.setPosition(act.CAMERA_WIDTH/2-prefectText.getWidth()/2, act.CAMERA_HEIGH/3);
                          PlayScene.this.attachChild(prefectText);
                          final IEntityModifierListener en = new IEntityModifierListener() {
							
							@Override
							public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
								
								
							}
							
							@Override
							public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
								prefectText.detachSelf();
								
							}
						};
                       
                          prefectText.registerEntityModifier(
                        		 
                        		   new DelayModifier(1.0f, en)
       
                          );
                        		
                        		 
						
						act.boom_perfectSound.play();
						prefectNum++;
						continue_prefectNum++;
						if(isStartPrefect)
						{
							start_prefect = 1;
							 if(act.mainScene.mission.getType().equals("109"))
							    	missdidNum+=1;
						}
						if(act.mainScene.mission.getType().equals("105"))
						    	missdidNum +=1;
						 if(act.mainScene.mission.getType().equals("106"))
							 missdidNum +=1;
						 if(act.mainScene.mission.getType().equals("111"))
							 missdidNum =continue_prefectNum;
						 if(act.mainScene.mission.getType().equals("112"))
							 missdidNum =prefectNum;
						
						act.todayPrefectNum++;
						act.totalPrefectNum++;
						act.todayMaxScore+=act.mainScene.getStyle().getScore();
						act.totalMaxScore+=act.mainScene.getStyle().getScore();
					}
					else
					{
						act.boomSound.play();
						continue_prefectNum = 0;
					}
					if(score>act.mainScene.maxScore)
					{
						act.mainScene.maxScore = score;
				       
					}
					isStartPrefect = false;
					
					  if(act.mainScene.mission.getType().equals("101"))
					    	missdidNum += act.mainScene.getStyle().getScore();
					    if(act.mainScene.mission.getType().equals("102"))
					    	missdidNum += act.mainScene.getStyle().getScore();
					    
					    if(act.mainScene.mission.getType().equals("103"))
					    	missdidNum += act.mainScene.getStyle().getScore();
						

					    if(act.mainScene.mission.getType().equals("107"))
					    	missdidNum =  (int)((System.currentTimeMillis() - play_start_time)/1000);
					    if(act.mainScene.mission.getType().equals("108"))
					    	missdidNum =+ (int)((System.currentTimeMillis() - play_start_time)/1000);

					   
					   if(missdidNum>=missNeedNum){
						    act.recordSound.play();
						     TextureRegion missionTexture = act.ur3.get(23); 
							 final float missionX = (act.CAMERA_WIDTH-missionTexture.getWidth())/2;
							 if(missionbackground == null){
							 missionbackground = new Sprite(missionX,act.CAMERA_HEIGH-200,missionTexture,act.getVertexBufferObjectManager());
							 missionbackground.setColor(GameConstant.UNDER_COLOR_R,GameConstant.UNDER_COLOR_G,GameConstant.UNDER_COLOR_B);
							
							 PlayScene.this.attachChild(missionbackground);
							 }
							 if(missionText == null){
						 	 String missionString = GameConstant.MISSIONNAME+act.mainScene.getMissionId()+GameConstant.MISSIONNUM_PID;
						 
						 	 Font missionFont = FontFactory.create(act.getFontManager(), act.getTextureManager(), 
									512, 512, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20, Color.WHITE.getARGBPackedInt());
						     missionFont.load();
							
						 	 missionText = new Text(missionbackground.getX()+50,missionbackground.getY(),
							 missionFont, missionString,Character.MAX_VALUE, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
						     missionText.setPosition(missionbackground.getX()+50, missionbackground.getY()+5);
						     PlayScene.this.attachChild(missionText);
							 }
							
						     Font scoreOfmissionFont = FontFactory.create(act.getFontManager(),act.getTextureManager(), 
									1024, 1024, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 30, Color.WHITE.getARGBPackedInt());
						     scoreOfmissionFont.load();
						     if(scoreOfmissionText == null){
						     String missionType =  act.mainScene.missis.get(act.mainScene.getMissionId()-1).getType();
						     String desc = "";
						    
						     for (int i = 0; i <act.mainScene.missiTypes.size(); i++) {
								if(act.mainScene.missiTypes.get(i).getType().equals(missionType))
									desc = act.mainScene.missiTypes.get(i).getDesc();
							 }
						   
						     scoreOfmissionText = new Text(missionbackground.getX()+50,missionbackground.getY()+50,
									 scoreOfmissionFont, desc.replace("{0}",act.mainScene.needNum+""),Character.MAX_VALUE, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
						     PlayScene.this.attachChild(scoreOfmissionText);
						     }
							 if(didMissionText == null){
						  
						     String didString = act.mainScene.needNum+"/"+act.mainScene.needNum;
						    
						     didMissionText = new Text(0,385,
						    		 scoreOfmissionFont, didString,Character.MAX_RADIX, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
						     didMissionText.setPosition(PlayScene.this.missionbackground.getX()+PlayScene.this.missionbackground.getWidth()-didMissionText.getWidth()-50, PlayScene.this.missionbackground.getY()+PlayScene.this.missionbackground.getHeight()/2);
					       
						     PlayScene.this.attachChild(didMissionText);
						     }

					   }
		    
				 }
				
				if(PlayScene.this.getPlayRole().getY()<=0||PlayScene.this.playRole.getY()>=act.CAMERA_HEIGH-PlayScene.this.getPlayRole().getHeight())
				{  
					
					
					
					if(PlayScene.this.istrue&&act.mEng.isRunning()) {
						act.deathSound.play();
						PlayScene.this.playRole.detachSelf();
                        particleStart_time = System.currentTimeMillis();
						
						final CircleParticleEmitter particleEmitter = new CircleParticleEmitter(PlayScene.this.getPlayRole().getX(),PlayScene.this.getPlayRole().getY(), 30);
						particleSystem = new SpriteParticleSystem(particleEmitter, 50, 50, 250, act.mParticleTextureRegion, act.getVertexBufferObjectManager());
						particleSystem.addParticleInitializer(new ColorParticleInitializer<Sprite>(act.mainScene.style.getTarget_r(),act.mainScene.style.getTarget_g(),act.mainScene.style.getTarget_b()));
						particleSystem.addParticleInitializer(new AccelerationParticleInitializer<Sprite>(-350,350,-350,350) );
						particleSystem.addParticleInitializer(new RotationParticleModifier<Sprite>(0,1, 0.0f, 360.0f));
						particleSystem.addParticleInitializer(new ScaleParticleModifier<Sprite>(0,1, 0.6f,2f));
						particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0, 0.5f, 0, 1));
						particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0.5f,1, 1, 0));
						PlayScene.this.attachChild(particleSystem);
						TextureRegion Texture = act.ur3.get(39); 
						alertW = null;
						alertW = new Sprite(act.CAMERA_WIDTH/2,act.CAMERA_HEIGH/2,Texture,act.getVertexBufferObjectManager());
						alertW.setPosition(act.CAMERA_WIDTH/2-alertW.getWidth()/2, act.CAMERA_HEIGH/2-alertW.getHeight()/2);
						PlayScene.this.attachChild(alertW);
						TextureRegion Texturetext = act.ur1.get(34); 
						text = null;
						text = new Sprite(act.CAMERA_WIDTH/2,act.CAMERA_HEIGH/2,Texturetext,act.getVertexBufferObjectManager());
						text.setPosition(act.CAMERA_WIDTH/2-text.getWidth()/2, alertW.getY()+alertW.getHeight()/10);
						PlayScene.this.attachChild(text);
						TextureRegion Textureclose = act.ur3.get(0);
					    close = null;
						close = new Sprite(alertW.getX()+alertW.getWidth(),0,Textureclose,act.getVertexBufferObjectManager()){
							 public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)

						        {
						            if (touchEvent.isActionUp())

						            {

						            
						            	if(act.mEng.isRunning()) {
						            		
						            	    act.todayPlayGameNum++;
						            	    act.totalPlayGameNum++;
						            		act.mainScene.restScene();
						        			
						        			 act.mEng.setScene(act.mainScene);
						    			} else {
						    			    act.todayPlayGameNum++;
						            	    act.totalPlayGameNum++;
						            		
						    				act.mEng.start();
						    				act.mainScene.restScene();
						        			
						        			 act.mEng.setScene(act.mainScene);
						    			}
						    			return true;

						            }

						            return true;

						        }
							};

						close.setPosition(alertW.getX()+alertW.getWidth()-close.getWidth()*2, alertW.getY()+close.getHeight()/2);
						close.setColor(Color.WHITE);
						close.setScale(1.5f);
						PlayScene.this.attachChild(close);
						PlayScene.this.registerTouchArea(close);
						TextureRegion TextureContinue = act.ur3.get(3);
						ContinueBackground = null;
						ContinueBackground = new Sprite(act.CAMERA_WIDTH/2,text.getY()+text.getHeight()+alertW.getHeight()/10,TextureContinue,act.getVertexBufferObjectManager()){
							 public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)

						        {
						            if (touchEvent.isActionUp())

						            {

						            	if(act.mEng.isRunning()) {
						            		ContinueBackground.detachSelf();
						            		 alertW.detachSelf();
						            		 text.detachSelf();
						            		 close.detachSelf();
						            		 
						            		 continueText.detachSelf();
						            		 HintText.detachSelf();
						            		 PlayScene.this.getTargetRole().clearEntityModifiers();
						 					 PlayScene.this.targetRole.registerEntityModifier(
						 							new SequenceEntityModifier(
						 							new MoveModifier(0.5f, PlayScene.this.targetRole.getWidth()-50,  PlayScene.this.targetRole.getWidth()-50 , -PlayScene.this.getTargetRole().getHeight(), act.CAMERA_HEIGH/3),
						 		                   
						 							new LoopEntityModifier(
						 							
						 							new SequenceEntityModifier(
						 									new MoveModifier(2, PlayScene.this.targetRole.getWidth()-50,act.CAMERA_WIDTH-PlayScene.this.targetRole.getWidth()-50,act.CAMERA_HEIGH/3,act.CAMERA_HEIGH/3,EaseSineInOut.getInstance()),
						 									new MoveModifier(2, act.CAMERA_WIDTH-PlayScene.this.targetRole.getWidth()-50,PlayScene.this.targetRole.getWidth()-50, act.CAMERA_HEIGH/3,act.CAMERA_HEIGH/3,EaseSineInOut.getInstance())
						 							)
						 							)
						 					));
						 					PlayScene.this.playRole.detachSelf();
						 					PlayScene.this.playRole.clearEntityModifiers();
											PlayScene.this.playRole.setPosition(hit1_X, hit1_Y);
						 					setPlayRole(PlayScene.this.playRole);
						 					 PlayScene.this.istrue = true;
						            		 
						    			} else {
						    				 alertW.detachSelf();
						            		 text.detachSelf();
						            		 close.detachSelf();
						            		 ContinueBackground.detachSelf();
						            		 continueText.detachSelf();
						            		 HintText.detachSelf();
						    				PlayScene.this.getTargetRole().clearEntityModifiers();
											PlayScene.this.targetRole.registerEntityModifier(
													new SequenceEntityModifier(
													new MoveModifier(0.5f, PlayScene.this.targetRole.getWidth()-50,  PlayScene.this.targetRole.getWidth()-50 , -PlayScene.this.getTargetRole().getHeight(), act.CAMERA_HEIGH/3),
								                   
													new LoopEntityModifier(
													
													new SequenceEntityModifier(
															new MoveModifier(2, PlayScene.this.targetRole.getWidth()-50,act.CAMERA_WIDTH-PlayScene.this.targetRole.getWidth()-50,act.CAMERA_HEIGH/3,act.CAMERA_HEIGH/3,EaseSineInOut.getInstance()),
															new MoveModifier(2, act.CAMERA_WIDTH-PlayScene.this.targetRole.getWidth()-50,PlayScene.this.targetRole.getWidth()-50, act.CAMERA_HEIGH/3,act.CAMERA_HEIGH/3,EaseSineInOut.getInstance())
													)
													)
											));
											PlayScene.this.playRole.detachSelf();
											PlayScene.this.playRole.clearEntityModifiers();
											PlayScene.this.playRole.setPosition(hit1_X, hit1_Y);
						 					setPlayRole(PlayScene.this.playRole);
											PlayScene.this.istrue = true;
						    				act.mEng.start();
						    				
						    			}
						    			return true;

						            }

						            return true;

						        }
							};
							
						ContinueBackground.setPosition(act.CAMERA_WIDTH/2-ContinueBackground.getWidth()/2,text.getY()+text.getHeight()+alertW.getHeight()/10);
					    
						PlayScene.this.attachChild(ContinueBackground);
						PlayScene.this.registerTouchArea(ContinueBackground);
						Font continueFont = FontFactory.create(act.getFontManager(), act.getTextureManager(), 
									200, 100, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 40, Color.WHITE.getARGBPackedInt());
						continueFont.load();
						HintText = null;
						continueText = new Text(0,0,
								continueFont, "继续",4, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
						continueText.setPosition(act.CAMERA_WIDTH/2-continueText.getWidth()/2,ContinueBackground.getY()+ContinueBackground.getHeight()/2-continueText.getHeight()/2);
						PlayScene.this.attachChild(continueText);
				        
						Font HintFont = FontFactory.create(act.getFontManager(), act.getTextureManager(), 
								400, 100, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20, Color.WHITE.getARGBPackedInt());
					    HintFont.load();
					    HintText = null;
						HintText = new Text(0,0,
								   HintFont, "立即复活并且得分翻倍，无需购买",20, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
						   HintText.setPosition(act.CAMERA_WIDTH/2-HintText.getWidth()/2,ContinueBackground.getY()+ContinueBackground.getHeight()/2+alertW.getHeight()/10);
						   
						   PlayScene.this.attachChild(HintText);
		
						PlayScene.this.getPlayRole().detachSelf();
						
						isaddParticle = true;
						playTime = (System.currentTimeMillis() - play_start_time)/1000;
						act.mainScene.playData.setScore(score);
						act.mainScene.playData.setPlay_one_game_score(score);
						act.mainScene.playData.setPlay_addup_game_score(act.mainScene.playData.getPlay_addup_game_score()+score);						
                        act.mainScene.playData.setPlay_game_num(act.mainScene.playData.getPlay_game_num()+1);

                        act.mainScene.playData.setPlay_one_game_prefectNum(prefectNum);
                        
						act.mainScene.playData.setPlay_addup_game_prefectNum(act.mainScene.playData.getPlay_addup_game_prefectNum()+prefectNum);
						
						act.mainScene.playData.setPlay_one_game_time(playTime);
						
						act.mainScene.playData.setPlay_addup_game_time(act.mainScene.playData.getPlay_addup_game_time()+playTime);
						
						act.mainScene.playData.setPrefect_start_num(act.mainScene.playData.getPrefect_start_num()+start_prefect);
						
						act.mainScene.playData.setPlay_continue_game_num(act.mainScene.playData.getPlay_continue_game_num()+1);
						
						act.mainScene.playData.setGet_continue_prefectNum(continue_prefectNum);
						
						act.mainScene.playData.setGet_prefectNum(act.mainScene.playData.getGet_prefectNum()+prefectNum);
						
                        act.mainScene.score = score;
                        PlayScene.this.getTargetRole().clearEntityModifiers();
                        PlayScene.this.istrue = false;
                        

	
			    } 	
		   }
        }
		});
	}

}
